package dsk.demo.product.controller.gupiao;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import dsk.demo.common.utils.PageQuery;
import dsk.demo.common.utils.R;
import dsk.demo.product.config.TonghuashunJob;
import dsk.demo.product.config.判断低估;
import dsk.demo.product.dao.GGoodGupiaoDao;
import dsk.demo.product.dao.GWeekPriceDao;
import dsk.demo.product.entity.gupiao.GGoodGupiao;
import dsk.demo.product.entity.gupiao.GWeekPrice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;

@RestController
@RequestMapping("/gupiao")
public class GuPiaoController {


    @Autowired
    private GGoodGupiaoDao goodGupiaoDao;
    @Autowired
    private GWeekPriceDao weekPriceDao;

    @GetMapping("/list")
    public R list(GGoodGupiao gGoodGupiao, PageQuery query) {
        IPage<GGoodGupiao> gGoodGupiaoIPage = goodGupiaoDao.selectPage(query.build(), Wrappers.<GGoodGupiao>lambdaQuery()
                .like(StrUtil.isNotBlank(gGoodGupiao.getGName()), GGoodGupiao::getGName, gGoodGupiao.getGName())
                .like(StrUtil.isNotBlank(gGoodGupiao.getGCode()), GGoodGupiao::getGCode, gGoodGupiao.getGCode())
                .likeRight(StrUtil.isNotBlank(gGoodGupiao.getGCodePre()), GGoodGupiao::getGCode, gGoodGupiao.getGCodePre())
                .eq(gGoodGupiao.getDate() != null, GGoodGupiao::getDate, gGoodGupiao.getDate())
                .orderByDesc(GGoodGupiao::getDate)
                .orderByAsc(GGoodGupiao::getGCode)
        );
        return R.ok(gGoodGupiaoIPage);
    }

    @PostMapping("testGuPiaoData")
    public String testGuPiaoData() {
//        GWeekPrice zuori = weekPriceDao.selectOne(Wrappers.<GWeekPrice>lambdaQuery()
//                .orderByDesc(GWeekPrice::getTime)
//                .last("limit 1")
//        );
        TonghuashunJob bean = SpringUtil.getBean(TonghuashunJob.class);
        bean.TengxunGetWeekEnd();
//        ArrayList<String> suerList = new ArrayList<>();
//        suerList.add("DaiShiKun");
//        suerList.add("ChenDong");
//        suerList.add("ChenRuoYu");
//        bean.sendMail("今日行情即将开始, 昨日数据..." + JSONUtil.toJsonStr(zuori, JSONConfig.create().setDateFormat("yyyy-MM-dd")),
//                suerList
//        );
        return "";
    }

    @PostMapping("testGuPiaoDigu")
    public String testGuPiaoDigu() {
        判断低估 bean = SpringUtil.getBean(判断低估.class);
        bean.定时任务();
        return "";
    }
}
